package weaver.interfaces.expense;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * 同步汇率
 * Created by Administrator on 2017/6/13.
 */
public class SynHuiLv extends BaseCronJob {

    @Override
    public void execute() {
        try{
            Log log = LogFactory.getLog(SynHuiLv.class);
            log.error("==============同步汇率开始=========");
    //获取最新美元
            float meiyuan = Float.parseFloat(Jsoup.connect("http://qq.ip138.com/hl.asp?from=USD&to=CNY&q=1").userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).get().getElementsByTag("td").last().text());;
            //获取最新欧元
            float ouyuan = Float.parseFloat(Jsoup.connect("http://qq.ip138.com/hl.asp?from=EUR&to=CNY&q=1").userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).get().getElementsByTag("td").last().text());;
            //获取最新英镑
            float yingbang = Float.parseFloat(Jsoup.connect("http://qq.ip138.com/hl.asp?from=GBP&to=CNY&q=1").userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).get().getElementsByTag("td").last().text());;
            //获取最新日元
            float riyuan = Float.parseFloat(Jsoup.connect("http://qq.ip138.com/hl.asp?from=JPY&to=CNY&q=1").userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).get().getElementsByTag("td").last().text());;

            String sql = null;
            RecordSet rs = new RecordSet();
            if (0l != meiyuan){
                sql = "update uf_bzll set ll = '"+meiyuan+"' where bzx = '1'";
                rs.execute(sql);
                log.error("同步美元汇率成功，最新汇率："+ouyuan);
            }
            if (0l != ouyuan){
                sql = "update uf_bzll set ll = '"+ouyuan+"' where bzx = '2'";
                rs.execute(sql);
                log.error("同步欧元汇率成功，最新汇率："+meiyuan);
            }
            if (0l != yingbang){
                sql = "update uf_bzll set ll = '"+yingbang+"' where bzx = '3'";
                rs.execute(sql);
                log.error("同步英镑汇率成功，最新汇率："+yingbang);
            }
            if (0l != riyuan){
                sql = "update uf_bzll set ll = '"+riyuan+"' where bzx = '4'";
                rs.execute(sql);
                log.error("同步日元汇率成功，最新汇率："+riyuan);
            }
            rs = null;
            sql = null;
            log = null;
            System.gc();
            log.error("==============同步汇率结束=========");
            return;
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
    }
}
